home *** CD-ROM | disk | FTP | other *** search
/ C/C++ Users Group Library 1996 July / C-C++ Users Group Library July 1996.iso / vol_200 / 241_01 / runrouti.ci < prev    next >
Text File  |  1987-08-29  |  4KB  |  193 lines

  1. /*
  2. HEADER:         CUG241;
  3. TITLE:          Inference Engine for Expert System;
  4. DATE:           12/30/85;
  5. VERSION:
  6. DESCRIPTION:   "Source code for inference engine for an Expert System.";
  7. KEYWORDS:       Artificial Intelligence, expert systems, inference engine;
  8. SYSTEM:         MS-DOS or UNIX System V;
  9. FILENAME:       RUNROUTI.C;
  10. WARNINGS:      "User-supported, non-commercial"
  11. AUTHORS:        George Hageman; 
  12. COMPILERS:      Microsoft C V3.00 or UNIX System V Portable C Compiler;
  13. REFERENCES:     ;
  14. ENDREF
  15. */
  16.  
  17. /*****************************************************************
  18. **                                **
  19. **      Inference -- (C) Copyright 1985 George Hageman    **
  20. **                                **
  21. **        user-supported software:                **
  22. **                                **
  23. **            George Hageman                **
  24. **            P.O. Box 11234                **
  25. **            Boulder, Colorado 80302            **
  26. **                                **
  27. *****************************************************************/
  28.  
  29. /*****************************************************************
  30. **
  31. **    runRoutine(antecedent)
  32. **
  33. **    spawns the process with the path name specified in
  34. **
  35. **    ruleBuff[antecedent].string
  36. **
  37. **    and returns the value as TRUE or FALSE  depending on
  38. **    the value returned from the exit() command from the spawnd routine.
  39. **
  40. **    the routine returns TRUE if there is any problem with spawning
  41. **    the specified executable.
  42. **
  43. ******************************************************************/
  44. #define    MAX_ARGS    20
  45.  
  46. #include <stdio.h>
  47.  
  48. #ifdef MSDOS
  49. #include <process.h>
  50. #endif
  51.  
  52. #include <errno.h>
  53.  
  54. #include "expert.h"
  55. #include "inference.h"
  56. #include "routine.h"
  57.  
  58. int    runRoutine(cnsquent)
  59.     int    cnsquent ;
  60. {
  61. extern int errno ;
  62.  
  63. #ifdef UNIXSV
  64. int    pnid ;
  65. #endif
  66.  
  67. int    i,argc,p_value,numChars ;
  68. char    buffer[MAX_STR_LEN], *string_p;
  69. char    *argv[MAX_ARGS] ;
  70.  
  71. /*
  72. ** copy string to buffer to get parameters
  73. */
  74.  
  75. string_p = &strBuff[ruleBuff[cnsquent].string] ;
  76.  
  77. for (numChars = 0 ; numChars < MAX_STR_LEN ; numChars++)
  78.     {
  79.     buffer[numChars]= *( string_p + numChars ) ; 
  80.     if(buffer[numChars] == NULL)
  81.         break ;
  82.     }
  83.     
  84. #ifdef DEBUG
  85. printf("\nDEBUG -- runroutine -- copied string is %s\n",buffer) ;
  86. #endif
  87.  
  88. /*
  89. ** set the argv(alues) to the proper location within the buffer
  90. */
  91.  
  92. argc = 1 ;
  93. argv[0] = buffer ;
  94.  
  95. for(i=0;i<numChars;i++)
  96.     {
  97. #ifdef DEBUG
  98. printf("\n DEBUG -- runroutine parameters i = %d, char = %c \n",i,buffer[i]) ;
  99. #endif
  100.     if(buffer[i] == NULL)
  101.         {
  102.         break ;
  103.         }
  104.     if(buffer[i] == BLANK)
  105.         {
  106.         buffer[i] = NULL ;
  107.         while( buffer[++i] == BLANK ) ;
  108.         if(buffer[i] == NULL)
  109.             {
  110.             break ;
  111.             }
  112.         argv[argc++] = &buffer[i] ;
  113.         if(argc == MAX_ARGS)
  114.             {
  115.             printf("\n maximum arguments exceeded for %s\n",string_p);
  116.             argc -= 1 ;
  117.             break ;
  118.             }
  119.         }
  120.     }
  121. argv[argc]=NULL ;
  122. #ifdef DEBUG
  123. for( i = 0 ; i < argc ; i++)
  124.     {
  125.     printf("\n argv[%d] = %s\n",i,argv[i]) ;
  126.     }
  127.  
  128. printf("\nRunning Routine %s ",argv[0] ) ;
  129. #endif
  130.  
  131. #ifdef MSDOS
  132. p_value=spawnv(P_WAIT,argv[0],argv ) ;
  133. #endif
  134.  
  135.  
  136. #ifdef UNIXSV
  137. pnid = fork() ;
  138. if(pnid == -1)
  139.     {
  140.     printf("\nFork failure! Running %s -- returning TRUE \n",argv[0]) ;
  141.     return(TRUE) ;
  142.     }
  143. if(pnid != 0 )         /* parent */
  144.     {
  145.     wait(&p_value) ;
  146.     if(p_value != -1)
  147.         p_value = (p_value >> 8 ) & 0x0ff ;
  148.     }
  149. else            /* child */
  150.     {
  151.     execv(argv[0],argv) ;
  152.     }
  153. #endif
  154.  
  155. /*
  156. **    The return value is set by the routine having an exit(X)  where
  157. **    X is the value to be returned...
  158. **    There is a problem since the return value can also provide an
  159. **    indication that there was some problem with the attempt as follows:
  160. */
  161.  
  162. if(p_value == RETURN_ROUTINE_TRUE)
  163.     {
  164. #ifdef DEBUG
  165.     printf(" -- TRUE\n") ;
  166. #endif
  167.     return(TRUE) ;
  168.     }
  169. if(p_value == RETURN_ROUTINE_FALSE)
  170.     {
  171. #ifdef DEBUG
  172.     printf(" -- FALSE \n") ;
  173. #endif
  174.     return(FALSE) ;
  175.     }
  176. if(p_value)
  177.     switch(errno)
  178.         {
  179.         case ENOENT :
  180.             printf("\n Executable file %s not found assumed TRUE\n",argv[0]) ;
  181.             return(TRUE) ;
  182.         case ENOEXEC :
  183.             printf("\n File %s is not executable assumed TRUE\n",argv[0]) ;
  184.             return(TRUE) ;
  185.         case ENOMEM :
  186.             printf("\ Not enough memory to run -- assumed TRUE\n") ;
  187.             return(TRUE) ;
  188.         }    
  189. printf("\n Routine did not return either ROUTINE_TRUE or ROUTINE_FALSE assumed TRUE\n") ;
  190. return(TRUE) ;
  191. }
  192.  
  193.